package com.kgc.sportscircle.controller;

import com.kgc.sportscircle.pojo.Malluseraddress;
import com.kgc.sportscircle.pojo.Sportsuser;
import com.kgc.sportscircle.redis.LoginPrefix;
import com.kgc.sportscircle.redis.RedisService;
import com.kgc.sportscircle.service.MalluseraddressService;
import com.kgc.sportscircle.utils.GetCookie;
import com.kgc.sportscircle.utils.Page;
import com.kgc.sportscircle.vo.MalluseraddressParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 描述： 控制器
 * @author 杨海文
 * @date 2019/07/22
 */
@RestController
@RequestMapping("/malluseraddress")
public class MalluseraddressController{

    @Reference(version ="1.11")
    private MalluseraddressService malluseraddressService;

    @Resource
    RedisService redisService;

    @RequestMapping("/findById.json")
    public Malluseraddress findById(Integer id){
        Malluseraddress result = null;
        try {
            result = malluseraddressService.findById(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：获取所有的Malluseraddress对象
     */
    @RequestMapping("/findAll.json")
    public List<Malluseraddress> findAll(){
        List<Malluseraddress> result = null;
        try {
            result = malluseraddressService.findAll();
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：新增保存Malluseraddress对象
     */
    @RequestMapping("/save.json")
    public Map save(Malluseraddress malluseraddress){
        Map result = new HashMap();
        result.put("flag",false);
        result.put("msg","保存失败");
        try {
            if(malluseraddressService.save(malluseraddress)>0){
                result.put("flag",true);
                result.put("msg","保存成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：根据主键删除对象
     * @param id
     */
    @RequestMapping("/delete.json")
    public Map delete(Integer id){
        Map result = new HashMap();
        result.put("flag",false);
        result.put("msg","保存失败");
        try {
            if(malluseraddressService.delete(id)>0){
                result.put("flag",true);
                result.put("msg","保存成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 描述：更新对象
     */
    @RequestMapping("/update.json")
    public Map update(Malluseraddress malluseraddress){
        Map result = new HashMap();
        result.put("flag",false);
        result.put("msg","更新失败");
        try {
            if( malluseraddressService.update(malluseraddress)>0){
                result.put("flag",true);
                result.put("msg","更新成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 根据不确定的条件，动态查询
     */
    @RequestMapping("/query.json")
    public List<Malluseraddress> query(MalluseraddressParam param){
        List<Malluseraddress> result = null;
        try {
            result = malluseraddressService.query(param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据不确定的条件，动态分页查询
     */
    @RequestMapping("/queryByPage.json")
    public Page queryByPage(MalluseraddressParam param){
        Page result = null;
        try {
            result = malluseraddressService.queryByPage(param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询地址
     * @param
     * @return
     */
    @RequestMapping("/selectAddress")
    public List<Malluseraddress> selectAddressByUserId(HttpServletRequest request) {
        System.out.println("****************&&&&&&&&&&&&&&&(((((((((");
        String token =  GetCookie.getSportsUserofCookie(request);
        Sportsuser sportsuser = redisService.get(LoginPrefix.getLoginList,token, Sportsuser.class);
        return malluseraddressService.selectAddressByUserId(sportsuser.getId());
    }
}
